package com.zhy.controller;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;
import com.github.qcloudsms.SmsSingleSender;
import com.github.qcloudsms.SmsSingleSenderResult;
import com.github.qcloudsms.httpclient.HTTPException;
import com.zhy.component.PhoneRandomBuilder;
import com.zhy.config.TencentSMSConfigure;
import com.zhy.redis.StringRedisServiceImpl;
import net.sf.json.JSONException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.io.IOException;

/**
 * @author: zhangocean
 * @Date: 2018/6/4 15:03
 * Describe: 注册获得手机验证码
 */
@Controller
public class GetPhoneCodeControl {

    @Autowired
    StringRedisServiceImpl stringRedisService;

    private static final String REGISTER = "register";

    @PostMapping("/getCode")
    @ResponseBody
    public int getAuthCode(HttpServletRequest request){

        String phone = request.getParameter("phone");
        String sign = request.getParameter("sign");
        String trueMsgCode = PhoneRandomBuilder.randomBuilder();
        System.out.println("验证手机号：" + trueMsgCode);

//        在redis中保存手机号验证码并设置过期时间
        stringRedisService.set(phone, trueMsgCode);
        stringRedisService.expire(phone, 300);

        String msgCode = "SMS_136394413";
        //注册的短信模板
        if(REGISTER.equals(sign)){
            msgCode = "SMS_136394413";
        }
        //改密码的短信模板
        else {
            msgCode = "SMS_139982667";
        }

        sendMesModel(phone,trueMsgCode);

        return 1;
    }

    public static SendSmsResponse sendSmsResponse(String phoneNumber, String code, String msgCode) throws ClientException {

        //可自助调整超时时间
        System.setProperty("sun.net.client.defaultConnectTimeout", "10000");
        System.setProperty("sun.net.client.defaultReadTimeout", "10000");
        //"***"分别填写自己的AccessKey ID和Secret
        IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", "LTAI4FuDhguJwxc3yyiqw8ua", "FgKtQutFPNE9H5nkP0Jupiicu2Eo7c");
        DefaultProfile.addEndpoint("cn-hangzhou", "cn-hangzhou", "Dysmsapi", "dysmsapi.aliyuncs.com");
        IAcsClient acsClient = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        //填写接收方的手机号码
        request.setPhoneNumbers(phoneNumber);
        //此处填写已申请的短信签名
        request.setSignName("填你的短信签名");
        //此处填写获得的短信模版CODE
        request.setTemplateCode(msgCode);
        //笔者的短信模版中有${code}, 因此此处对应填写验证码
        request.setTemplateParam("{\"code\":\"" + code + "\"}");
        SendSmsResponse sendSmsResponse = acsClient.getAcsResponse(request);

        return sendSmsResponse;
    }
/**
      * 自定义短信内容发送
      * @param msg 短信内容
      * @param number 用户手机号
      * @return OK 成功  null 失败
      */
public static String sendMess(String msg , String number){
try { SmsSingleSender ssender = new SmsSingleSender(TencentSMSConfigure.APP_ID, TencentSMSConfigure.APP_KEY);
SmsSingleSenderResult result = ssender.send(0, "86", number,
msg, "", "");
System.out.print(result);
return result.errMsg;
} catch (HTTPException e) {
 // HTTP响应码错误
e.printStackTrace();
} catch (JSONException e) {
 // json解析错误
e.printStackTrace();
} catch (IOException e) {
// 网络IO错误e.printStackTrace();
}
    return null;
}

/**
      * 指定模板ＩＤ发送短信
      * @param number 用户手机号
      * @return OK 成功  null 失败
      */
public static String sendMesModel(String number , String trueMsgCode){ try {
String[] params = {trueMsgCode , "1" };//{参数}
SmsSingleSender ssender = new SmsSingleSender(TencentSMSConfigure.APP_ID, TencentSMSConfigure.APP_KEY);
SmsSingleSenderResult result = ssender.sendWithParam("86", number,
        TencentSMSConfigure.TEMPLATE_ID, params, TencentSMSConfigure.SMS_SIGN, "", ""); // 签名参数未提供或者为空时，会使用默认签名发送短信
System.out.print(result);
return result.errMsg;//OK
} catch (HTTPException e) {
// HTTP响应码错误
 e.printStackTrace();
} catch (JSONException e) {
// json解析错误
 e.printStackTrace();
} catch (IOException e) {
 // 网络IO错误
e.printStackTrace();
}
return null;
}



}
